<?php


require_once("Common/Model/CheckStrings.php");
require_once("Database/Model/ExerciseDAL.php");
require_once("Database/Model/MuscleGroupDAL.php");

class ExerciseModel
{

	/**
    *@var ExerciseDAL
    **/	
	private $exerciseDAL;
	/**
    *@var CheckStrings
    **/	
	private $checkStrings;
	/**
    *@var MuscleGroupDAL
    **/	
	private $muscleGroupDAL;
	/**
    *@var string errorMessage
    **/	
	private static $errorMessage = "ExerciseModel::ErrorMessage";
	/**
    *@var string succesMessage
    **/	
	private static $succesMessage = "ExerciseModel::succesMessage";

    private static $idToEdit = "ExerciseModel::IdToEdit";
	
	public function __construct($idToEdit = 0)
	{
		$this->exerciseDAL = new ExerciseDAL();
		$this->checkStrings = new CheckStrings();
		$this->muscleGroupDAL = new MuscleGroupDAL();
		if($idToEdit != 0)
		{
			$_SESSION[self::$idToEdit] = $idToEdit;
		}
	}

	/**
    *@return int
    **/	
	public function getIdToEdit()
	{
		if(isset($_SESSION[self::$idToEdit]))
		{
			return $_SESSION[self::$idToEdit];
		}
	} 

	public function resetErrorMessages()
	{
		$_SESSION[self::$errorMessage] = "";
		$_SESSION[self::$succesMessage] = "";
	}

	/**
    *@param int
    *@param string
    *@param string
    *@return boolean
    **/	
	public function ValidateExercise($muscleGroupID, $eName, $eDescription)
	{
		if($this->checkIfMuscleGroupIDExists($muscleGroupID))
		{
			$validEName = $this->ValidateEName($eName);
			$validEDescription = $this->ValidateEDescription($eDescription);
			
			if($validEName && $validEDescription &&
			   $this->checkForTags($eName, $eDescription))
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		return false;

	}

	/**
    *@param int
    *@param string
    *@param string
    *@param int
    *@return boolean
    **/	
	public function CreateAndSaveExercise($muscleGroupID, $eName, $eDescription,$id = null)
	{
		$arr = $this->muscleGroupDAL->getMuscleGroupList();
		$muscleGroup = "";
		foreach($arr as $value) 
		{
			if($muscleGroupID == $value->getMuscleGroupID())
			{
				$muscleGroup = $value->getMusclegroup();
			}
		}
	    $exerciseToSave = Exercise::createNewExercise($muscleGroup,$muscleGroupID, $eName, $eDescription);
		if($id != null)
		{
			$this->exerciseDAL->saveNewExercise($exerciseToSave, $id);
			$_SESSION[self::$succesMessage] = "Övningen '$eName' har redigerats";
		}
		else
		{
			$this->exerciseDAL->saveNewExercise($exerciseToSave);	
			$_SESSION[self::$succesMessage] = "Övningen '$eName' har lagts till";
		}
		
	}

	/**
    *@param string
    *@param string
    *@return boolean
    **/	
	private function checkForTags($eName, $eDescription)
	{
		$validEName = $this->checkStrings->hasTags($eName);
		$validEDescription = $this->checkStrings->hasTags($eDescription);
		
		if($validEName)
		{
			return false;
			$_SESSION[self::$errorMessage] .= "   Övningsnamnet innehåller ogiltiga tecken, tex '< >'   .";
		}
		elseif($validEDescription)
		{
			return false;
			$_SESSION[self::$errorMessage] .= "   Beskrivningen innehåller ogiltiga tecken, tex '< >'   .";
		}
		return true;
	}


	/**
    *@param string
    *@return boolean
    **/	
	private function ValidateEName($eName)
	{
		if(strlen($eName) < 1) 
		{
			$_SESSION[self::$errorMessage] .= "   Ett övningsnamn måste fyllas i   .";
			return false;
		}
		if(strlen($eName) > 40) 
		{
			$_SESSION[self::$errorMessage] .= "   Övningsnamnet får max vara 40tecken   .";
			return false;
		}
		return true;
	}	

	/**
    *@param string
    *@return boolean
    **/	
	private function ValidateEDescription($eDescription)
	{
		if(strlen($eDescription) < 1) 
		{
			$_SESSION[self::$errorMessage] .= "   En Beskrivningen måste fyllas i   .";
			return false;
		}
		if(strlen($eDescription) > 200) 
		{
			$_SESSION[self::$errorMessage] .= "   Beskrivningen får max vara 200tecken   .";
			return false;
		}
		return true;
	}


	/**
    *@param int
    *@return boolean
    **/	
	private function checkIfMuscleGroupIDExists($muscleID)
	{
		$arr = $this->muscleGroupDAL->getMuscleGroupList();
		foreach($arr as $value) 
		{
			if($muscleID == $value->getMuscleGroupID())
			{
				return true;
			}
		}
		$_SESSION[self::$errorMessage] .= "   Ett fel uppstod, har du valt en muskelgrupp?   .";
		return false;
	}


	/**
    *@return string session
    **/	
	public function getSuccesMessage()
	{
		if(isset($_SESSION[self::$succesMessage]))
		{
			$ret = $_SESSION[self::$succesMessage];
			$_SESSION[self::$succesMessage] = "";
			return $ret; 
		}
		return "";
	}

	/**
    *@return string session
    **/	
	public function getErrorMessage()
	{
		if(isset($_SESSION[self::$errorMessage]))
		{
			return $_SESSION[self::$errorMessage]; 
		}
		return "";
	}


	/**
	*@param int
    **/	
	public function ValidateAndDeleteExercise($exerciseID)
	{
		$exercise = $this->exerciseDAL->getExerciseList($exerciseID);
		if(isset($exercise))
		{
			if($exercise->CheckIfSameID($exerciseID))
			{
				$this->exerciseDAL->DeleteExercise($exerciseID);
				$_SESSION[self::$succesMessage] = "Övningen har tagits bort";
			}
		}
		$_SESSION[self::$errorMessage] = "   Ett fel uppstod vid borttagning av övningen   .";
	
	}

}